import java.awt.Graphics;
import java.awt.TextArea;
import java.io.IOException;
import java.net.UnknownHostException;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;


public class FrameProjeto extends JFrame implements Runnable{
	
	private JComboBox jcbParticipante;
	private JComboBox jcbItemParticipante;
	private JComboBox jcbMeuItem;
	private TextArea jtaMsgs;
	private JCheckBox jcheckbEfetProv;
	private JLabel lblQuantidade;
	
	private Colecionador colecionador;
	
	
	private JRadioButton rdbtnParaInicio = new JRadioButton("P\u00E1ra no Inicio");
	private JRadioButton rdbtnParaMeio = new JRadioButton("P\u00E1ra no meio");
	private JRadioButton rdbtnParaFim = new JRadioButton("P\u00E1ra no fim");
	
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		try{
			
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			String nome =  JOptionPane.showInputDialog((String)"Coloque o seu nome:");
			FrameProjeto fp= new FrameProjeto(nome);
			fp.setVisible(true);
			fp.buscaColecionador();
			Thread t = new Thread(fp);
			t.start();
			
		}catch (Exception e){
			e.printStackTrace();
		}


	}
	public synchronized void informaUser(String msg){
		jtaMsgs.setText(jtaMsgs.getText()+msg+"\n");
		jtaMsgs.setCaretPosition(jtaMsgs.getText().length());		
	}
	private synchronized void limpaTela(){
		jtaMsgs.setText("");		
	}
	/**
	 * Create the application.
	 * @throws AlreadyBoundException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public FrameProjeto(String nome) throws AlreadyBoundException, IOException {
		initialize(nome);
		colecionador = new Colecionador(nome, this);
	}
	
	
	public void buscaColecionador() throws AccessException, RemoteException, InterruptedException{
		
		informaUser("Fazendo busca de outros colecionadores");
		ArrayList <String> nomes = colecionador.buscaNomeColecionadores();
		informaUser("Busca finalizada");
		
		iniciaDadosComboBox(nomes);
		
		Thread.sleep(2000);
		
	}
	private void iniciaDadosComboBox(ArrayList <String> nomes){
		
		for(String s : nomes){
			jcbParticipante.addItem(s);
		}
		
		for(Colecao c : colecionador.getColecoes()){
			jcbItemParticipante.addItem(c.getNome());
			jcbMeuItem.addItem(c.getNome());
		}
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(String nome) {
		setBounds(100, 100, 465, 624);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(nome);
		setResizable(true);
		
		initializeComponents();
		
	}
	
	public boolean getEfetProvisoria(){
		return jcheckbEfetProv.isSelected();
	}
	
	private void initializeComponents(){
		
		JPanel contentPane = new JPanel();
		contentPane.setBackground(UIManager.getColor("Button.background"));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblTroca = new JLabel("Troca");
		lblTroca.setBounds(10, 11, 46, 14);
		contentPane.add(lblTroca);
		
		JLabel lblCons = new JLabel("Area de Mensagens");
		lblCons.setBounds(10, 230, 198, 14);
		contentPane.add(lblCons);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 217, 429, 2);
		contentPane.add(separator);
		
		jcbParticipante = new JComboBox();
		jcbParticipante.setBounds(10, 61, 93, 20);
		contentPane.add(jcbParticipante);
		
		JLabel lblNewLabel = new JLabel("Participante");
		lblNewLabel.setBounds(10, 36, 93, 14);
		contentPane.add(lblNewLabel);
		
		jcbItemParticipante = new JComboBox();
		jcbItemParticipante.setBounds(115, 61, 93, 20);
		contentPane.add(jcbItemParticipante);
		
		JLabel lblItemParticipante = new JLabel("Item participante");
		lblItemParticipante.setBounds(115, 36, 93, 14);
		contentPane.add(lblItemParticipante);
		
		JLabel lblMeuItem = new JLabel("Meu Item");
		lblMeuItem.setBounds(218, 36, 93, 14);
		contentPane.add(lblMeuItem);
		
		jcbMeuItem = new JComboBox();
		jcbMeuItem.setBounds(218, 61, 122, 20);
		contentPane.add(jcbMeuItem);
		
		JButton btnConfirmar = new JButton("Confirmar");
		btnConfirmar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					colecionador.iniciaTroca(colecionador.getNome()+" "+jcbParticipante.getSelectedItem()+" "+
							jcbMeuItem.getSelectedItem()+ " "+jcbItemParticipante.getSelectedItem());
				} catch (Exception e){
					e.printStackTrace();
				}
			}
		});
		btnConfirmar.setBounds(350, 60, 89, 23);
		contentPane.add(btnConfirmar);
		
		jtaMsgs = new TextArea();
		jtaMsgs.setEditable(false);
		jtaMsgs.setBounds(10, 250, 330, 327);
		contentPane.add(jtaMsgs);
		
		JButton btnConsultar = new JButton("Consultar");
		btnConsultar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					colecionador.iniciaConsulta();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnConsultar.setBounds(350, 554, 89, 23);
		contentPane.add(btnConsultar);
		
		jcheckbEfetProv = new JCheckBox("Efetiva\u00E7\u00E3o Provis\u00F3ria");
		jcheckbEfetProv.setSelected(true);
		jcheckbEfetProv.setBounds(10, 128, 127, 23);
		contentPane.add(jcheckbEfetProv);
		
		JLabel lbl1 = new JLabel("Quantidade: ");
		lbl1.setBounds(218, 92, 63, 14);
		contentPane.add(lbl1);
		
		lblQuantidade = new JLabel("");
		lblQuantidade.setBounds(291, 92, 49, 14);
		contentPane.add(lblQuantidade);
		
		JButton btnLimparTela = new JButton("Limpar Tela");
		btnLimparTela.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				limpaTela();
			}
		});
		btnLimparTela.setBounds(350, 520, 89, 23);
		contentPane.add(btnLimparTela);
		
		rdbtnParaInicio = new JRadioButton("P\u00E1ra no Inicio");
		rdbtnParaInicio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
			}
		});
		rdbtnParaInicio.setBounds(139, 128, 109, 23);
		contentPane.add(rdbtnParaInicio);
		
		rdbtnParaMeio = new JRadioButton("P\u00E1ra no meio");
		rdbtnParaMeio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colecionador.setStopMeio(rdbtnParaMeio.isSelected());
			}
		});
		rdbtnParaMeio.setBounds(139, 154, 109, 23);
		contentPane.add(rdbtnParaMeio);
		
		rdbtnParaFim = new JRadioButton("P\u00E1ra no fim");
		rdbtnParaFim.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
			}
		});
		rdbtnParaFim.setBounds(139, 180, 109, 23);
		contentPane.add(rdbtnParaFim);
		
		
		
	}
	@Override
	public void run() {
		
		while(true){
			if(jcbMeuItem.getItemCount()!=0){
				
				lblQuantidade.setText(""+colecionador.buscaColecao((String)jcbMeuItem.getSelectedItem()).getQuantidade());
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
